package com.bxd.baodan.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ProductItemAction;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.item.PAllSortItem;
import com.bxd.baodan.item.ProductItem;
import com.bxd.baodan.item.ProductSortStatus;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.widget.MyPopupWindow;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class AllProduceActivity extends BaseListActivity<ProductItem> {
    private TextView p_sort;
    private TextView p_crowd;
    private TextView p_type;
    private TextView pCompany;


    private ListView popListView;
    private MyPopupWindow popMenu;


    private CommonAdapter<ProductSortStatus> sortAdapter, crowdAdapter, typeAdapter;

    public List<ProductSortStatus> listSort = new ArrayList<>();
    public List<ProductSortStatus> listCrowd = new ArrayList<>();
    public List<ProductSortStatus> listType = new ArrayList<>();

    private static final int SORT_FLAG = 1;
    private static final int CROWD_FLAG = 2;
    private static final int TYPE_FLAG = 3;

    String sortId, crowdId, typeId;
    String companyId = "0";

    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_all_product;
    }

    @Override
    protected void preDone() {
        setActionBarTitle("产品列表");
        ImageView barSou = (ImageView) findViewById(R.id.iv_bar_right);
        barSou.setImageResource(R.drawable.sousuo_);
        barSou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MobclickAgent.onEvent(App.getContext(), "v5_5_1_CP_ss");
                openActivity(SearchProduceActivity.class);
            }
        });


        p_sort = (TextView) findViewById(R.id.p_sort);
        p_crowd = (TextView) findViewById(R.id.p_crowd);
        p_type = (TextView) findViewById(R.id.p_type);
        pCompany = (TextView) findViewById(R.id.p_company);


        p_sort.setOnClickListener(this);
        p_crowd.setOnClickListener(this);
        p_type.setOnClickListener(this);
        pCompany.setOnClickListener(this);

        initPopMenu();
        autoRefresh();
    }


    @Override
    public void setNetUtil() {
        // TODO: 2017/7/17 修改接口格式
        netUtil = new NetUtil("product/lists", this);


    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<ProductItem>>() {
        }.getType();
    }


    @Override
    public int getItemLayout() {
        return R.layout.product_item_noused;
    }

    @Override
    public void convert(ViewHolder holder, ProductItem item) {

        ProductItemAction.action(this, holder, item,"all_produce");
    }

    public CommonAdapter<ProductSortStatus> getAdapter(List<ProductSortStatus> list, final TextView tv, final int flag) {
        return new CommonAdapter<ProductSortStatus>(this, list, R.layout.left_right_item) {
            @Override
            public void convert(final ViewHolder holder, final ProductSortStatus item) {


                if (tv.getText().equals(item.title)) {
                    holder.setText(R.id.left_tv, item.title);
                    ((TextView) holder.getView(R.id.left_tv)).setTextColor(Color.parseColor("#37b059"));
                    holder.setImageResource(R.id.right_iv, R.drawable.p_checked);

                    if (flag == SORT_FLAG) {
                        sortId = item.id;
                    } else if (flag == CROWD_FLAG) {
                        crowdId = item.id;
                    } else if (flag == TYPE_FLAG) {
                        typeId = item.id;
                    }

                } else {
                    holder.setText(R.id.left_tv, item.title);
                    ((TextView) holder.getView(R.id.left_tv)).setTextColor(getResources().getColor(R.color.question_list_text_1));
                    holder.setImageResource(R.id.right_iv, R.color.transparent);
                }
                holder.getView(R.id.left_iv).setVisibility(View.GONE);
                holder.getView(R.id.l_r_layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv.setText(item.title);
                        if (flag == SORT_FLAG) {

                            sortId = item.id;
                        } else if (flag == CROWD_FLAG) {

                            crowdId = item.id;
                        } else if (flag == TYPE_FLAG) {

                            typeId = item.id;
                        }

                        popMenu.dismiss();
                        netUtil.clearParams();
                        netUtil.setParams("sort", sortId);
                        netUtil.setParams("crowd ", crowdId);
                        netUtil.setParams("type ", typeId);
                        netUtil.setParams("company ", companyId);
                        autoRefresh();
                    }
                });
            }
        };
    }

    private void initMenuData() {

        sortAdapter = getAdapter(listSort, p_sort, SORT_FLAG);
        crowdAdapter = getAdapter(listCrowd, p_crowd, CROWD_FLAG);
        typeAdapter = getAdapter(listType, p_type, TYPE_FLAG);

        NetUtil conditionNet = new NetUtil("product/condition", this);
        conditionNet.postRequest("", new RequestStringListener() {
            @Override
            public void onComplete(String result) {
                if (JSONUtil.isSuccess(result)) {
                    PAllSortItem allSortItem = new Gson().fromJson(result, PAllSortItem.class);
                    listCrowd.addAll(allSortItem.data.crowd);
                    listSort.addAll(allSortItem.data.sort);
                    listType.addAll(allSortItem.data.type);


                    p_sort.setText(listSort.get(0).title);
                    p_crowd.setText(listCrowd.get(0).title);
                    p_type.setText(listType.get(0).title);

                    sortId = listSort.get(0).id;
                    crowdId = listCrowd.get(0).id;
                    typeId = listType.get(0).id;
                    companyId = "0";


                    sortAdapter.notifyDataSetChanged();
                    crowdAdapter.notifyDataSetChanged();
                    typeAdapter.notifyDataSetChanged();
                }
            }
        });


    }

    @Override
    public void onClick(View v) {
        Drawable drawable = getResources().getDrawable(R.drawable.p_arrow_down_green);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        switch (v.getId()) {


            case R.id.p_sort:

                MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zcp_px");
                p_sort.setTextColor(Color.parseColor("#37b059"));
                p_sort.setCompoundDrawables(null, null, drawable, null);
                popListView.setAdapter(sortAdapter);
                popListView.setDivider(getResources().getDrawable(R.color.bg_gray_main));
                popListView.setDividerHeight(1);
                // 解决7.0pop定位问题
                popMenu.showAsDropDown(findViewById(R.id.line));
                break;
            case R.id.p_crowd:
                MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zcp_rq");
                p_crowd.setCompoundDrawables(null, null, drawable, null);
                p_crowd.setTextColor(Color.parseColor("#37b059"));
                popListView.setAdapter(crowdAdapter);
                //                popListView.setDivider(getResources().getDrawable(R.color.bg_gray_main));
                //                popListView.setDividerHeight(1);
                popMenu.showAsDropDown(findViewById(R.id.line));


                break;
            case R.id.p_type:
                MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zcp_xz");

                p_type.setCompoundDrawables(null, null, drawable, null);
                p_type.setTextColor(Color.parseColor("#37b059"));
                popListView.setAdapter(typeAdapter);
                popListView.setDivider(getResources().getDrawable(R.color.bg_gray_main));
                popListView.setDividerHeight(1);
                popMenu.showAsDropDown(findViewById(R.id.line));
                break;
            case R.id.p_company:

                MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zcp_gs");
                drawable = getResources().getDrawable(R.drawable.p_check_company);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                pCompany.setCompoundDrawables(null, null, drawable, null);
                pCompany.setTextColor(Color.parseColor("#37b059"));
                openActivityForResult(AllCompanyActivity.class, new String[]{"company_name"}, new String[]{pCompany.getText().toString()});
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    private void initPopMenu() {
        initMenuData();
        View contentView = View.inflate(this, R.layout.popwin_supplier_list,
                null);

        popMenu = new MyPopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        popMenu.setOutsideTouchable(true);
        popMenu.setBackgroundDrawable(new BitmapDrawable());
        popMenu.setFocusable(true);
        popMenu.setAnimationStyle(R.style.popwin_anim_style);
        popMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                Drawable drawable = getResources().getDrawable(R.drawable.p_arrow_down_gray);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                p_sort.setTextColor(getResources().getColor(R.color.question_list_text_1));
                p_type.setTextColor(getResources().getColor(R.color.question_list_text_1));
                p_crowd.setTextColor(getResources().getColor(R.color.question_list_text_1));
                p_sort.setCompoundDrawables(null, null, drawable, null);
                p_type.setCompoundDrawables(null, null, drawable, null);
                p_crowd.setCompoundDrawables(null, null, drawable, null);
            }
        });

        popListView = (ListView) contentView
                .findViewById(R.id.popwin_supplier_list_lv);
        contentView.findViewById(R.id.popwin_supplier_list_bottom)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        popMenu.dismiss();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Drawable drawable = getResources().getDrawable(R.drawable.p_uncheck_company);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        pCompany.setCompoundDrawables(null, null, drawable, null);
        pCompany.setTextColor(getResources().getColor(R.color.question_list_text_1));
        if (resultCode == 200) {
            pCompany.setText(data.getStringExtra("company_name"));
            companyId = data.getStringExtra("company_id");
            if (companyId == null) {
                companyId = "0";
            }
            netUtil.clearParams();
            netUtil.setParams("sort", sortId);
            netUtil.setParams("crowd ", crowdId);
            netUtil.setParams("type ", typeId);
            netUtil.setParams("company ", companyId);
            autoRefresh();
        }
    }


}
